package com.zhongshengnetwork.rightbe.dbservice;

import com.zhongshengnetwork.rightbe.Constant.Constant;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.dbmodel.AddressbookdbModel;
import com.zhongshengnetwork.rightbe.dbmodel.NotifydbModel;
import com.zhongshengnetwork.rightbe.dbmodel.ShopCardbModel;

import org.xutils.DbManager;
import org.xutils.db.sqlite.SqlInfo;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.db.table.DbModel;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;

/**
 * Created by zhongsheng-luojianlan on 16/11/26.
 */

public class NotifydbService {
    //插入或者更新数据
    public static boolean saveOrUpdate(NotifydbModel model){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            db.saveOrUpdate(model);
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //获取数据
    public static List<NotifydbModel> getData(String userid, int pageindex, int pagerecord){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (pagerecord <= 0 || pageindex < 0){
                return null;
            }
            if (pageindex > 0){
                pageindex = pageindex*pagerecord;
            }
            if (db.selector(NotifydbModel.class).where("userid","==",userid).orderBy("time",true).limit(pagerecord).offset(pageindex) != null)
               return db.selector(NotifydbModel.class).where("userid","==",userid).orderBy("time",true).limit(pagerecord).offset(pageindex).findAll();
        } catch (DbException e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }

    //删除数据
    public static boolean deleteById(long id){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            db.deleteById(NotifydbModel.class,id);
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //删除全部
    public static boolean deleteAll(String userid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            db.delete(NotifydbModel.class, WhereBuilder.b("userid", "==", userid));
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //获取未读数量
    public static long getUnReadCount(String userid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.selector(NotifydbModel.class).where("userid","==",userid).and("state","==", 0).and("msgtype",">=",0) != null)
              return db.selector(NotifydbModel.class).where("userid","==",userid).and("state","==", 0).and("msgtype",">=",0).count();
        } catch (DbException e) {
            e.printStackTrace();
            return 0;
        }
        return 0;
    }

    //标注全部为已读
    public static boolean changeToReadState(String userid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.selector(NotifydbModel.class).where("userid","==",userid).and("state","==",0) != null){
                List<NotifydbModel> list = db.selector(NotifydbModel.class).where("userid","==",userid).and("state","==",0).findAll();
                if (list != null && list.size() > 0){
                    for (NotifydbModel model : list){
                        model.setState(1);
                        db.saveOrUpdate(model);
                    }
                }
            }
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //兼容旧数据
    public static boolean updateOldData(String userid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.selector(NotifydbModel.class).where("msgtype","==",null) != null){
                List<NotifydbModel> list = db.selector(NotifydbModel.class).where("msgtype","==",null).findAll();
                if (list != null && list.size() > 0){
                    for (NotifydbModel model : list){
                        model.setMsgtype(Constant.MessageListType.DefaultMsgType);
                        db.saveOrUpdate(model);
                    }
                }
            }
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //获取某类消息未读数量
    public static long getUnReadCountByType(String userid, int msgtype){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.selector(NotifydbModel.class).where("userid","==",userid).and("state","==", 0).and("msgtype","==",msgtype) != null)
                return db.selector(NotifydbModel.class).where("userid","==",userid).and("state","==", 0).and("msgtype","==",msgtype).count();
        } catch (DbException e) {
            e.printStackTrace();
            return 0;
        }
        return 0;
    }


    //把某类消息标注为已读
    public static boolean changeToReadStateByType(String userid, int msgtype){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.selector(NotifydbModel.class).where("userid","==",userid).and("state","==",0).and("msgtype","==",msgtype) != null){
                List<NotifydbModel> list = db.selector(NotifydbModel.class).where("userid","==",userid).and("state","==",0).and("msgtype","==",msgtype).findAll();
                if (list != null && list.size() > 0){
                    for (NotifydbModel model : list){
                        model.setState(1);
                        db.saveOrUpdate(model);
                    }
                }
            }
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //获取某类消息
    public static List<NotifydbModel> getDataByType(String userid, int pageindex, int pagerecord, int msgtype){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (pagerecord <= 0 || pageindex < 0){
                return null;
            }
            if (pageindex > 0){
                pageindex = pageindex*pagerecord;
            }
            if (db.selector(NotifydbModel.class).where("userid","==",userid).and("msgtype","==",msgtype).orderBy("time",true).limit(pagerecord).offset(pageindex) != null)
                return db.selector(NotifydbModel.class).where("userid","==",userid).and("msgtype","==",msgtype).orderBy("time",true).limit(pagerecord).offset(pageindex).findAll();
        } catch (DbException e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }


    //删除某类消息
    public static boolean deleteAllByType(String userid, int msgtype){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            db.delete(NotifydbModel.class, WhereBuilder.b("userid", "==", userid).and("msgtype","==",msgtype));
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }



}
